home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / cmd.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  8KB  |  328 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import string
  5. __all__ = [
  6.     'Cmd']
  7. PROMPT = '(Cmd) '
  8. IDENTCHARS = string.ascii_letters + string.digits + '_'
  9.  
  10. class Cmd:
  11.     prompt = PROMPT
  12.     identchars = IDENTCHARS
  13.     ruler = '='
  14.     lastcmd = ''
  15.     intro = None
  16.     doc_leader = ''
  17.     doc_header = 'Documented commands (type help <topic>):'
  18.     misc_header = 'Miscellaneous help topics:'
  19.     undoc_header = 'Undocumented commands:'
  20.     nohelp = '*** No help on %s'
  21.     use_rawinput = 1
  22.     
  23.     def __init__(self, completekey = 'tab', stdin = None, stdout = None):
  24.         import sys as sys
  25.         if stdin is not None:
  26.             self.stdin = stdin
  27.         else:
  28.             self.stdin = sys.stdin
  29.         if stdout is not None:
  30.             self.stdout = stdout
  31.         else:
  32.             self.stdout = sys.stdout
  33.         self.cmdqueue = []
  34.         self.completekey = completekey
  35.  
  36.     
  37.     def cmdloop(self, intro = None):
  38.         self.preloop()
  39.         if self.use_rawinput and self.completekey:
  40.             
  41.             try:
  42.                 import readline as readline
  43.                 self.old_completer = readline.get_completer()
  44.                 readline.set_completer(self.complete)
  45.                 readline.parse_and_bind(self.completekey + ': complete')
  46.             except ImportError:
  47.                 pass
  48.             except:
  49.                 None<EXCEPTION MATCH>ImportError
  50.             
  51.  
  52.         None<EXCEPTION MATCH>ImportError
  53.         
  54.         try:
  55.             if intro is not None:
  56.                 self.intro = intro
  57.             
  58.             if self.intro:
  59.                 self.stdout.write(str(self.intro) + '\n')
  60.             
  61.             stop = None
  62.             while not stop:
  63.                 None if self.cmdqueue else None<EXCEPTION MATCH>EOFError
  64.                 self.stdout.write(self.prompt)
  65.                 self.stdout.flush()
  66.                 line = self.stdin.readline()
  67.                 if not len(line):
  68.                     line = 'EOF'
  69.                 else:
  70.                     line = line[:-1]
  71.                 line = self.precmd(line)
  72.                 stop = self.onecmd(line)
  73.                 stop = self.postcmd(stop, line)
  74.             self.postloop()
  75.         finally:
  76.             if self.use_rawinput and self.completekey:
  77.                 
  78.                 try:
  79.                     import readline as readline
  80.                     readline.set_completer(self.old_completer)
  81.                 except ImportError:
  82.                     pass
  83.                 except:
  84.                     None<EXCEPTION MATCH>ImportError
  85.                 
  86.  
  87.  
  88.  
  89.     
  90.     def precmd(self, line):
  91.         return line
  92.  
  93.     
  94.     def postcmd(self, stop, line):
  95.         return stop
  96.  
  97.     
  98.     def preloop(self):
  99.         pass
  100.  
  101.     
  102.     def postloop(self):
  103.         pass
  104.  
  105.     
  106.     def parseline(self, line):
  107.         line = line.strip()
  108.         if not line:
  109.             return (None, None, line)
  110.         elif line[0] == '?':
  111.             line = 'help ' + line[1:]
  112.         elif line[0] == '!':
  113.             if hasattr(self, 'do_shell'):
  114.                 line = 'shell ' + line[1:]
  115.             else:
  116.                 return (None, None, line)
  117.         
  118.         i = 0
  119.         n = len(line)
  120.         while i < n and line[i] in self.identchars:
  121.             i = i + 1
  122.         cmd = line[:i]
  123.         arg = line[i:].strip()
  124.         return (cmd, arg, line)
  125.  
  126.     
  127.     def onecmd(self, line):
  128.         (cmd, arg, line) = self.parseline(line)
  129.         if not line:
  130.             return self.emptyline()
  131.         
  132.         if cmd is None:
  133.             return self.default(line)
  134.         
  135.         self.lastcmd = line
  136.         if cmd == '':
  137.             return self.default(line)
  138.         else:
  139.             
  140.             try:
  141.                 func = getattr(self, 'do_' + cmd)
  142.             except AttributeError:
  143.                 return self.default(line)
  144.  
  145.             return func(arg)
  146.  
  147.     
  148.     def emptyline(self):
  149.         if self.lastcmd:
  150.             return self.onecmd(self.lastcmd)
  151.         
  152.  
  153.     
  154.     def default(self, line):
  155.         self.stdout.write('*** Unknown syntax: %s\n' % line)
  156.  
  157.     
  158.     def completedefault(self, *ignored):
  159.         return []
  160.  
  161.     
  162.     def completenames(self, text, *ignored):
  163.         dotext = 'do_' + text
  164.         return _[1]
  165.  
  166.     
  167.     def complete(self, text, state):
  168.         if state == 0:
  169.             import readline
  170.             origline = readline.get_line_buffer()
  171.             line = origline.lstrip()
  172.             stripped = len(origline) - len(line)
  173.             begidx = readline.get_begidx() - stripped
  174.             endidx = readline.get_endidx() - stripped
  175.             if begidx > 0:
  176.                 (cmd, args, foo) = self.parseline(line)
  177.             None if cmd == '' else None<EXCEPTION MATCH>AttributeError
  178.             compfunc = self.completenames
  179.             self.completion_matches = compfunc(text, line, begidx, endidx)
  180.         
  181.         
  182.         try:
  183.             return self.completion_matches[state]
  184.         except IndexError:
  185.             return None
  186.  
  187.  
  188.     
  189.     def get_names(self):
  190.         names = []
  191.         classes = [
  192.             self.__class__]
  193.         while classes:
  194.             aclass = classes.pop(0)
  195.             if aclass.__bases__:
  196.                 classes = classes + list(aclass.__bases__)
  197.             
  198.             names = names + dir(aclass)
  199.         return names
  200.  
  201.     
  202.     def complete_help(self, *args):
  203.         return self.completenames(*args)
  204.  
  205.     
  206.     def do_help(self, arg):
  207.         if arg:
  208.             
  209.             try:
  210.                 func = getattr(self, 'help_' + arg)
  211.             except AttributeError:
  212.                 
  213.                 try:
  214.                     doc = getattr(self, 'do_' + arg).__doc__
  215.                     if doc:
  216.                         self.stdout.write('%s\n' % str(doc))
  217.                         return None
  218.                 except AttributeError:
  219.                     pass
  220.  
  221.                 self.stdout.write('%s\n' % str(self.nohelp % (arg,)))
  222.                 return None
  223.  
  224.             func()
  225.         else:
  226.             names = self.get_names()
  227.             cmds_doc = []
  228.             cmds_undoc = []
  229.             help = { }
  230.             for name in names:
  231.                 if name[:5] == 'help_':
  232.                     help[name[5:]] = 1
  233.                     continue
  234.             
  235.             names.sort()
  236.             prevname = ''
  237.             for name in names:
  238.                 if name[:3] == 'do_':
  239.                     if name == prevname:
  240.                         continue
  241.                     
  242.                     prevname = name
  243.                     cmd = name[3:]
  244.                     if cmd in help:
  245.                         cmds_doc.append(cmd)
  246.                         del help[cmd]
  247.                     elif getattr(self, name).__doc__:
  248.                         cmds_doc.append(cmd)
  249.                     else:
  250.                         cmds_undoc.append(cmd)
  251.                 cmd in help
  252.             
  253.             self.stdout.write('%s\n' % str(self.doc_leader))
  254.             self.print_topics(self.doc_header, cmds_doc, 15, 80)
  255.             self.print_topics(self.misc_header, help.keys(), 15, 80)
  256.             self.print_topics(self.undoc_header, cmds_undoc, 15, 80)
  257.  
  258.     
  259.     def print_topics(self, header, cmds, cmdlen, maxcol):
  260.         if cmds:
  261.             self.stdout.write('%s\n' % str(header))
  262.             if self.ruler:
  263.                 self.stdout.write('%s\n' % str(self.ruler * len(header)))
  264.             
  265.             self.columnize(cmds, maxcol - 1)
  266.             self.stdout.write('\n')
  267.         
  268.  
  269.     
  270.     def columnize(self, list, displaywidth = 80):
  271.         if not list:
  272.             self.stdout.write('<empty>\n')
  273.             return None
  274.         
  275.         nonstrings = _[1]
  276.         size = len(list)
  277.         if size == 1:
  278.             self.stdout.write('%s\n' % str(list[0]))
  279.             return None
  280.         
  281.         for nrows in range(1, len(list)):
  282.             ncols = (size + nrows - 1) // nrows
  283.             colwidths = []
  284.             totwidth = -2
  285.             for col in range(ncols):
  286.                 colwidth = 0
  287.                 for row in range(nrows):
  288.                     i = row + nrows * col
  289.                     if i >= size:
  290.                         break
  291.                     
  292.                     x = list[i]
  293.                     colwidth = max(colwidth, len(x))
  294.                 
  295.                 colwidths.append(colwidth)
  296.                 totwidth += colwidth + 2
  297.                 if totwidth > displaywidth:
  298.                     break
  299.                     continue
  300.             
  301.             if totwidth <= displaywidth:
  302.                 break
  303.                 continue
  304.         else:
  305.             nrows = len(list)
  306.             ncols = 1
  307.             colwidths = [
  308.                 0]
  309.         for row in range(nrows):
  310.             texts = []
  311.             for col in range(ncols):
  312.                 i = row + nrows * col
  313.                 if i >= size:
  314.                     x = ''
  315.                 else:
  316.                     x = list[i]
  317.                 texts.append(x)
  318.             
  319.             while texts and not texts[-1]:
  320.                 del texts[-1]
  321.             for col in range(len(texts)):
  322.                 texts[col] = texts[col].ljust(colwidths[col])
  323.             
  324.             self.stdout.write('%s\n' % str('  '.join(texts)))
  325.         
  326.  
  327.  
  328.